package com.smart.campus.web.controller.school;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.core.page.TableDataInfo;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.ScBadge;
import com.smart.campus.school.service.IScBadgeService;
import com.smart.campus.system.domain.SysUser;
import com.smart.campus.web.utils.SchoolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 729002330@qq.com
 * @date :2020-05-26
 */
@Controller
@RequestMapping("/school/badge")
public class BadgeController extends BaseController {
    private String prefix = "school/badge";

    @Autowired
    private IScBadgeService scBadgeService;
    @RequiresPermissions("school:badge:view")
    @GetMapping("/{modeType}")
    public String badge(@PathVariable("modeType") Integer modeType)
    {
        logger.info("modeType："+modeType);
        return prefix + (modeType==1?"/badgeTeacher":"/badge");
    }

    /**
     * 新增校徽信息
     */
    @GetMapping("/add/{modeType}")
    public String add(@PathVariable("modeType") Integer modeType)
    {
        if(modeType>1){
            modeType = 0;
        }
        return prefix + (modeType==1?"/addTeacher":"/add");
    }


    /**
     * 新增保存校徽信息
     */
    @RequiresPermissions("school:badge:add")
    @Log(title = "校徽信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ScBadge badge)
    {
        ScBadge b0 = new ScBadge();
        b0.setUserId(badge.getUserId());
        b0.setStatus(badge.getStatus());
        List<ScBadge> list = scBadgeService.selectScBadgeList(b0);
        AjaxResult result = AjaxResult.success();
        if(list==null||list.size()==0){
            //保存校徽信息
            //Snowflake snowflake = IdUtil.createSnowflake(2, 2);
            badge.setBadgeNo(badge.getBadgeNo());
            badge.setCreateTime(DateUtils.getNowDate());
            SysUser currUser = ShiroUtils.getSysUser();
            badge.setCreateBy(currUser.getUserName());
            badge.setScId(SchoolUtil.getSchoolId());
            scBadgeService.insertScBadge(badge);
        }
        else{
            boolean bstudent = badge.getUserType()==2;
            String error = "该"+(bstudent?"学生【":"职工【")+badge.getUserName()+"】已经创建过校徽;";
            result =  AjaxResult.error(error);
        }
        return result;
    }

    /**
     * 修改校徽信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ScBadge badge = scBadgeService.selectScBadgeById(id);
        mmap.put("badge", badge);
        return prefix + (badge.getUserType()==1? "/editTeacher" : "/edit");
    }

    /**
     * 修改保存校徽信息
     */
    @RequiresPermissions("school:badge:edit")
    @Log(title = "校徽信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ScBadge badge)
    {
        return toAjax(scBadgeService.updateScBadge(badge));
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("school:badge:remove")
    @Log(title = "校徽信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scBadgeService.deleteScBadgeByIds(ids));
    }


    /**
     * 查询校徽信息列表
     */
    @RequiresPermissions("school:badge:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScBadge badge)
    {
        startPage();
        List<ScBadge> list = scBadgeService.selectScBadgeList(badge);
        return getDataTable(list);
    }

    /**
     * 查询校徽信息列表
     */
    @GetMapping("/query/{modeType}")
    public String query(@PathVariable("modeType") Integer modeType)
    {
        return prefix+(modeType==1?"/queryTeacher":"/query");
    }


    /**
     * 导出校徽信息列表
     */
    @RequiresPermissions("school:badge:export")
    @Log(title = "校徽信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScBadge badge)
    {
        List<ScBadge> list = scBadgeService.selectScBadgeList(badge);
        ExcelUtil<ScBadge> util = new ExcelUtil<ScBadge>(ScBadge.class);
        return util.exportExcel(list, "badge");
    }

}
